﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace MVVMS.Helpers
{
    public class FileStorage
    {
        readonly IsolatedStorageFile isoFile;

        public FileStorage()
        {
            isoFile = IsolatedStorageFile.GetUserStoreForApplication();
        }

        /// <summary>
        /// Saves an XML File
        /// </summary>
        /// <param name="xmlFile">Data to Save</param>
        /// <param name="fileName">Filename to save</param>
        public void SavetoFile(XDocument xmlFile, string fileName)
        {
            using (IsolatedStorageFileStream FileStream = isoFile.OpenFile(fileName, FileMode.Create, FileAccess.Write))
            {
                xmlFile.Save(FileStream);
            }
        }
        
        /// <summary>
        /// Loads an XML File
        /// </summary>
        /// <param name="fileName">file to Load</param>
        /// <returns>XDocument data</returns>
        public XDocument LoadfromFile(string fileName)
        {
            if (isoFile.FileExists(fileName))
            {
                using (IsolatedStorageFileStream fileStream = isoFile.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                {
                    return XDocument.Load(fileStream);
                }
            }
            return null;
        }

        /// <summary>
        /// Saves a Track File
        /// </summary>
        /// <param name="trackFile">Data to Save</param>
        /// <param name="fileName">Filename to save</param>
        public void SavetoFile(object dataObject, string fileName)
        {
            using (IsolatedStorageFileStream FileStream = isoFile.OpenFile(fileName, FileMode.Create, FileAccess.Write))
            {
                DataContractSerializer ser = new DataContractSerializer(dataObject.GetType());
                ser.WriteObject(FileStream, dataObject);
            }
        }

        /// <summary>
        /// Just check to see if a file exists
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool FileExists(string fileName)
        {
            return isoFile.FileExists(fileName);
        }

        /// <summary>
        /// Quick Function to Delete a File
        /// </summary>
        /// <param name="fileName"></param>
        public void DeleteFile(string fileName)
        {
            if (isoFile.FileExists(fileName))
                isoFile.DeleteFile(fileName);
        }

        /// <summary>
        /// Loads a  File
        /// </summary>
        /// <param name="fileName">file to Load</param>
        /// <returns>XDocument data</returns>
        public T LoadfromFile<T>(string fileName)
        {
            if (isoFile.FileExists(fileName))
            {
                using (IsolatedStorageFileStream fileStream = isoFile.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                {
                    DataContractSerializer ser = new DataContractSerializer(typeof(T));
                    return (T) ser.ReadObject(fileStream);
                }
            }
            return default(T);
        }
    }
}
